package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fz.entity.Topic;
import com.fz.mapper.TopicMapper;
import com.fz.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 存储话题信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {
    @Resource
    TopicMapper topicMapper;
    @Override
    public Boolean likeTopic(Long topicId) {
        UpdateWrapper<Topic> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("likecount = likecount + 1")
                .eq("id", topicId); // 假设BaseEntity中有id字段
        if(this.update(updateWrapper))
        {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<Topic> list(String title) {
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        wrapper.eq("title" , title)
                .eq("is_deleted" , 0)
                .orderByDesc("create_time");
        List<Topic> list = super.list(wrapper);
        return list;
    }

    @Override
    public Topic createTopic(Long userId, String title,String content) {
        Topic topic=new Topic(userId, title,content);
        this.save(topic);
        return topic;
    }
}
